﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")][Description("在指定位置生成一个NPC，无行为树逻辑直接载入预设体")]
    public class AddNpc : LatentActionNodeBase
    {

        private int uniqueId = -1;
        
        public override bool allowRoutineQueueing { get { return false; } }

        public IEnumerator Invoke(string npcName,int uId,string pointName)
        {
            npcName = npcName.Trim();
            pointName = pointName.Trim();
            
            yield return new WaitForOneFrame();

            BluePrintPoint point = BlueprintUtils.GetBlueprintPoint(pointName);
            if (point!=null)
            {
               BlueprintUtils.AddNpc(npcName,uId,point.position,point.rotation);
            }
            uniqueId = uId;
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<string>("npcName");
            var p2 = node.AddValueInput<int>("uniqueId");
            var p3 = node.AddValueInput<string>("pointName");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value,p2.value,p3.value), f); });
        }
        
        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (uniqueId > -1)
            {
                BlueprintUtils.RemoveNpc(uniqueId);
            }
        }
    }
}